package com.boot.web;

import com.alibaba.fastjson.JSONObject;
import com.boot.entity.User;
import com.boot.utils.JWTUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Chunsheng.Zhang
 * @date 2021/1/6 17:01
 * @description : TODO
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private JWTUtil jwtUtil ;



    /**
     *
     * 登录入口
     *
     *
     * */
    @PostMapping("/login")
    public User login (@RequestParam("userName") String userName,
                                     @RequestParam("passWord") String passWord,
                                     HttpServletResponse response){

        User user = new User();
        user.setPassWord(passWord);
        user.setUserName(userName);
        user.setId(1000L);
        String userStr = JSONObject.toJSONString(user);

        // 省略数据源校验
        String token = jwtUtil.getToken(userStr) ;
        if (!StringUtils.isEmpty(token)) {
            response.setHeader("token",token);
        }

        return user;
    }




    @GetMapping("/getCurrentUser")
    public User getCurrentUser(HttpServletRequest request){
        User currentUser = (User)request.getAttribute("currentUser");

        return currentUser;
    }


}
